﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Permission.Interface;


namespace Microhard.Permission_MembershipImpl
{
    public class UserOperationLogImpl : IUserOperationLog
    {
        public Core.DBUtility.ExecutedResult Add(Permission.Models.UserOperationLogModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            model.OperationLogID = Guid.NewGuid();
            var toAdd = model.ToEntity();

            db.Microhard_UserOperationLogs.InsertOnSubmit(toAdd);

            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("UserOperationLogImpl.Add", "添加日志失败");
                Log.Error("UserOperationLogImpl.Add", ex);
            }

            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Permission.Models.UserOperationLogModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Permission.Models.UserOperationLogModel model)
        {
            throw new NotImplementedException();
        }

        public Permission.Models.UserOperationLogModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Permission.Models.UserOperationLogModel GetModelByModel(Permission.Models.UserOperationLogModel model)
        {
            throw new NotImplementedException();
        }

        public List<Permission.Models.UserOperationLogModel> Query(Permission.Models.UserOperationLogModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            IEnumerable<Microhard_UserOperationLog> entities = null;
            if (!string.IsNullOrEmpty(Model.UserRealName))
            {
                entities = from b in db.Microhard_UserOperationLogs
                           where b.UserRealName.IndexOf(Model.UserRealName) > 0
                           select b;
            }
            else
            {
                entities = from b in db.Microhard_UserOperationLogs
                           select b;
            }

            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(p => p.ToModel()).ToList();
        }

        public List<Permission.Models.UserOperationLogModel> Query(Permission.Models.UserOperationLogModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
